/**
 * Project Name:springBoot-serviceImpl
 * File Name:MongoDBUserTestServiceImpl.java
 * Package Name:com.libra.serviceImp
 * Date:2018年8月8日下午8:27:28
 * Copyright (c) 2018, wangzhch All Rights Reserved.
 *
*/

package com.libra.serviceImp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.libra.model.mongoDb.Users;
import com.libra.service.MongoDBUserTestService;
import com.mongodb.client.result.DeleteResult;

/**
 * ClassName:MongoDBUserTestServiceImpl <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2018年8月8日 下午8:27:28 <br/>
 * @author   wangzhch
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
@Component  //泛指组件，当组件不好归类的时候，我们可以使用这个注解进行标注
public class MongoDBUserTestServiceImpl implements MongoDBUserTestService {
	@Autowired
	private MongoOperations mongoOperations;

	@Override
	public void saveUser(Users users) {
		mongoOperations.save(users);
	}

	@Override
	public Users findUserByPrimaryKey(String id) {
		return mongoOperations.findOne(new Query(Criteria.where("id").is(id)), Users.class);
	}

	@Override
	public Long removeUser(String id) {
		return mongoOperations.remove(new Query(Criteria.where("id").is(id)), Users.class).getDeletedCount();
	}

	@Override
	public Long updateUserByPrimaryKey(Users users) {
		return mongoOperations.updateFirst(new Query(Criteria.where("id").is(users.getId())), Update.update("name", users.getName()), Users.class).getMatchedCount();
	}

	@Override
	public List<Users> listUser() {
		List<Users> list = mongoOperations.findAll(Users.class);
		return list;
	}

	@Override
	public List<Users> listUser(String name) {
		List<Users> list = mongoOperations.find(new Query(Criteria.where("name").is(name)), Users.class);
		return list;
	}

}

